﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace ui
{
    public class _window : _control
    {
        string _text = "";

        Color _BorderColor = Color.Black;
        Color _BackgroundColor = Color.White;
        Color _CaptionColor = Color.Black;
        Color _ForeColor = Color.White;
        Color _EffectColor = Color.White;

        bool _Visible = true;
        bool _DrawBorder = false;
        bool _Fullscreen = false;
        bool _Gradient = false;
        bool _Effect = false;
        EffectModeEnum _EffectMode = EffectModeEnum.Squares;
        GradientModeEnum _GradientMode = GradientModeEnum.Blue;

        int GradientStart = 160;
        int GradientEnd = 50;

        public enum EffectModeEnum
        {
            Squares,
            Circles
        }

        public enum GradientModeEnum
        {
            Red,
            Green,
            Blue
        }

        public _window()
            : base()
        {
            type = ControlTypes.Window;
        }

        public override void setup(PictureBox p)
        {            
            base.setup(p);

            FullScreen = true;
            DrawBorder = true;
        }

        [Category("Data"), Description("Caption")]
        public string Caption
        {
            get { return _text; }
            set { _text = value; if (pic != null) pic.Invalidate(); }
        }

        [Category("Appearance"), Description("Effect Mode")]
        public EffectModeEnum EffectMode
        {
            get { return _EffectMode; }
            set { _EffectMode = value; if (pic != null) pic.Invalidate(); }
        }

        [Category("Appearance"), Description("Gradient Mode")]
        public GradientModeEnum GradientMode
        {
            get { return _GradientMode; }
            set { _GradientMode = value; if (pic != null) pic.Invalidate(); }
        }

        [Category("Appearance"), Description("Toggle Effect")]
        public bool Effect
        {
            get { return _Effect; }
            set { _Effect = value; if (pic != null) pic.Invalidate(); }
        }

        [Category("Appearance"), Description("Visible")]
        public bool Visible
        {
            get { return _Visible; }
            set { _Visible = value; }
        }

        [Category("Appearance"), Description("Toggle Gradient")]
        public bool Gradient
        {
            get { return _Gradient; }
            set { _Gradient = value; if (pic != null) pic.Invalidate(); }
        }

        [Category("Appearance"), Description("Toggle Fullscreen")]
        public bool FullScreen
        {
            get { return _Fullscreen; }
            set 
            {
                _Fullscreen = value;

                if (_Fullscreen)
                {
                    x = 0;
                    y = 0;

                    if (pic != null)
                    {
                        width = parent.settings.width;
                        height = parent.settings.height;
                    }
                    sizeable = false;
                }
                else
                {
                    x = 0;
                    y = 0;

                    if (pic != null)
                    {
                        width = 5 * parent.settings.charwidth;
                        height = 5 * parent.settings.charheight;
                    }
                    sizeable = true;
                }

                if (pic != null) pic.Invalidate();
            }
        }

        [Category("Appearance"), Description("Draw Border")]
        public bool DrawBorder
        {
            get { return _DrawBorder; }
            set { _DrawBorder = value; if (pic != null) pic.Invalidate(); }
        }

        [XmlElement(Type = typeof(_xmlcolor))]
        [Category("Appearance"), Description("Border Color")]
        public Color BorderColor
        {
            get { return _BorderColor; }
            set { _BorderColor = value; if (pic != null) pic.Invalidate(); }
        }

        [XmlElement(Type = typeof(_xmlcolor))]
        [Category("Appearance"), Description("Background Color")]
        public Color BackgroundColor
        {
            get { return _BackgroundColor; }
            set { _BackgroundColor = value; if (pic != null) pic.Invalidate(); }
        }

        [XmlElement(Type = typeof(_xmlcolor))]
        [Category("Appearance"), Description("Caption Color")]
        public Color CaptionColor
        {
            get { return _CaptionColor; }
            set { _CaptionColor = value; if (pic != null)pic.Invalidate(); }
        }

        [XmlElement(Type = typeof(_xmlcolor))]
        [Category("Appearance"), Description("Fore Color")]
        public Color ForeColor
        {
            get { return _ForeColor; }
            set { _ForeColor = value; if (pic != null)pic.Invalidate(); }
        }

        [XmlElement(Type = typeof(_xmlcolor))]
        [Category("Appearance"), Description("Effect Color")]
        public Color EffectColor
        {
            get { return _EffectColor; }
            set { _EffectColor = value; if (pic != null)pic.Invalidate(); }
        }

        public override void render(object sender, PaintEventArgs e)
        {
            g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;

            SizeF s = g.MeasureString(Caption, parent.mainFont);
            Brush fill = new SolidBrush(BackgroundColor);

            if (Gradient)
            {
                switch (GradientMode)
                {
                    case GradientModeEnum.Red: GradientY(g,GradientStart, 0, 0, GradientEnd, 0, 0, width, height, 4); break;
                    case GradientModeEnum.Green: GradientY(g,0, GradientStart, 0, 0, GradientEnd, 0, width, height, 4); break;
                    case GradientModeEnum.Blue: GradientY(g,0, 0, GradientStart, 0, 0, GradientEnd, width, height, 4); break;
                }
            }
            else
            {
                g.FillRectangle(fill, new Rectangle(0, 0, width, height));
            }

            if (Effect)
            {
                Random rnd = new Random();
                for (int i = 0; i < 30; i++)
                {
                    int tx = -20 + rnd.Next(width + 40);
                    int ty = -20 + rnd.Next(height + 40);
                    int tr = rnd.Next(40);

                    if (EffectMode == EffectModeEnum.Squares) g.DrawRectangle(new Pen(EffectColor),tx, ty, tx + tr, ty + tr);
                }
            }

            if (Caption != "")
            {
                g.FillRectangle(new SolidBrush(CaptionColor), new Rectangle(0, 0, width, 12));
                g.DrawString(Caption, parent.mainFont, new SolidBrush(ForeColor), 0, 0);
            }

            if (DrawBorder) g.DrawRectangle(new Pen(BorderColor), new Rectangle(0, 0, width - 1, height - 1));

            base.render(sender, e);
        }

        public void GradientY(Graphics g, int sr, int sg, int sb, int er, int eg, int eb, int w, int h, int step)
        {
            int y = 0;
            float incr = (float)(er - sr) / (h / step);
            float incg = (float)(eg - sg) / (h / step);
            float incb = (float)(eb - sb) / (h / step);

            float cr = sr;
            float cg = sg;
            float cb = sb;

            for (y = 0; y < h; y += step)
            {
                cr += incr;
                cg += incg;
                cb += incb;

                g.FillRectangle(new SolidBrush(Color.FromArgb((int)cr, (int)cg, (int)cb)),0, y, w, y + step);
            }
        }
    }
}
